#ifndef __TOUCH_H__
#define __TOUCH_H__
#include "sys.h"
#include "ott2001a.h"	    
#include "gt9147.h"	    
//////////////////////////////////////////////////////////////////////////////////	 

//触摸屏驱动（支持ADS7843/7846/UH7843/7846/XPT2046/TSC2046/OTT2001A等） 代码	   
//STM32F4工程模板-库函数版本
//淘宝店铺：http://mcudev.taobao.com							   
//********************************************************************************
//修改说明
//V1.1 20140721
//修正MDK在-O2优化时,触摸屏数据无法读取的bug.在TP_Write_Byte函数添加一个延时,解决问题.
//////////////////////////////////////////////////////////////////////////////////


#define TP_PRES_DOWN 0x80  //触屏被按下	  
#define TP_CATH_PRES 0x40  //有按键按下了 
 
//触摸屏控制器
typedef struct
{
	unsigned char (*init)(void);			//初始化触摸屏控制器
	unsigned char (*scan)(unsigned char);				//扫描触摸屏.0,屏幕扫描;1,物理坐标;	 
	void (*adjust)(void);		//触摸屏校准 
	unsigned short x[OTT_MAX_TOUCH]; 		//当前坐标
	unsigned short y[OTT_MAX_TOUCH];		//电容屏有最多5组坐标,电阻屏则用x[0],y[0]代表:此次扫描时,触屏的坐标,用
								//x[4],y[4]存储第一次按下时的坐标. 
	unsigned char  sta;					//笔的状态 
								//b7:按下1/松开0; 
	                            //b6:0,没有按键按下;1,有按键按下. 
								//b5:保留
								//b4~b0:电容触摸屏按下的点数(0,表示未按下,1表示按下)
/////////////////////触摸屏校准参数(电容屏不需要校准)//////////////////////								
	float xfac;					
	float yfac;
	short xoff;
	short yoff;	   
//新增的参数,当触摸屏的左右上下完全颠倒时需要用到.
//b0:0,竖屏(适合左右为X坐标,上下为Y坐标的TP)
//   1,横屏(适合左右为Y坐标,上下为X坐标的TP) 
//b1~6:保留.
//b7:0,电阻屏
//   1,电容屏 
	unsigned char touchtype;
}_m_tp_dev;

extern _m_tp_dev tp_dev;	 	//触屏控制器在touch.c里面定义

//电阻屏芯片连接引脚	   
#define PEN  		PCin(5)  	//T_PEN
#define DOUT 		PBin(14)   	//T_MISO
#define TDIN 		PBout(15)  	//T_MOSI
#define TCLK 		PBout(13)  	//T_SCK
#define TCS  		PBout(12)  	//T_CS  
   
//电阻屏函数
void TP_Write_Byte(unsigned char num);						//向控制芯片写入一个数据
unsigned short TP_Read_AD(unsigned char CMD);							//读取AD转换值
unsigned short TP_Read_XOY(unsigned char xy);							//带滤波的坐标读取(X/Y)
unsigned char TP_Read_XY(unsigned short *x,unsigned short *y);					//双方向读取(X+Y)
unsigned char TP_Read_XY2(unsigned short *x,unsigned short *y);					//带加强滤波的双方向坐标读取
void TP_Drow_Touch_Point(unsigned short x,unsigned short y,unsigned short color);//画一个坐标校准点
void TP_Draw_Big_Point(unsigned short x,unsigned short y,unsigned short color);	//画一个大点
void TP_Save_Adjdata(void);						//保存校准参数
unsigned char TP_Get_Adjdata(void);						//读取校准参数
void TP_Adjust(void);							//触摸屏校准
void TP_Adj_Info_Show(unsigned short x0,unsigned short y0,unsigned short x1,unsigned short y1,unsigned short x2,unsigned short y2,unsigned short x3,unsigned short y3,unsigned short fac);//显示校准信息
//电阻屏/电容屏 共用函数
unsigned char TP_Scan(unsigned char tp);								//扫描
unsigned char TP_Init(void);								//初始化
																 

 		  
#endif